//
//  ContentView.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/4.
//

import SwiftUI

struct ContentView: View {
    @State private var showCustomPopup = false
    @State private var showImagePopup = false
    @State private var showWindowPopup = false
    @State private var showBottomPopup = false
    @State private var selectedDate = Date()
    @State private var showDatePopup = false
    @State private var showEnumViewPopup = false
    @State private var showContactPicker = false
    @State private var selectedContact: String?
    
    @EnvironmentObject private var appState: NDAppState
    @Environment(\.navigationManager) var navManager
    
    var body: some View {
        let buttons: [NDPopupButton] = [
            NDPopupButton.init("Cancel", style: .default) {
                showCustomPopup = false
            },
            NDPopupButton.init("确认") {
                showCustomPopup = false
            }
        ]
        var showNextPop = false
        
        let navigationConfig = NavigationConfig(
            title: "Content",
        )
        
        NDBaseContentView(navigationConfig: navigationConfig) {
            ScrollView {
                VStack(spacing: 20) {
                    Button("通用 Alert（中间弹出）") {
                        showCustomPopup = true
                    }
                    .padding()
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(8)
                    
                    Button("自定义 Alert（中间弹出）") {
                        showImagePopup = true
                    }
                    .padding()
                    .background(Color.green)
                    .foregroundColor(.white)
                    .cornerRadius(8)
                    
                    Button("显示 ActionSheet（底部弹出）") {
                        showWindowPopup = true
                    }
                    .padding()
                    .background(Color.orange)
                    .foregroundColor(.white)
                    .cornerRadius(8)
                    
                    Button("Date picker: \(DateFormatterUtility.shared.string(from: selectedDate))") {
                        showDatePopup = true
                    }
                    .padding()
                    .background(Color.purple)
                    .foregroundColor(.white)
                    .cornerRadius(8)
                    .buttonStyle(FullSizeButtonStyle())
                    
                    Button("Window ActionSheet（底部弹出）") {
                        showBottomPopup = true
                    }
                    .padding()
                    .background(Color.purple)
                    .foregroundColor(.white)
                    .cornerRadius(8)
                    .buttonStyle(FullSizeButtonStyle())
                    
                    Button("enumView") {
                        showEnumViewPopup = true
                        //                    withAnimation(.easeInOut(duration: 0.3)) {
                        //                        SMLoginInfo.shared.logout()
                        //                    }
                        
                        //                    appState.rootView = .login
                        
                        //                    let pushViewItem = NavigationViewManager.PushViewItem(tag: "home", animated: true) {
                        //                        AnyView(
                        //                            SMLoginView()
                        //                        )
                        //
                        //
                        //                    }
                        //                    NotificationCenter.default.post(name: .NavigationViewManagerPushView, object: pushViewItem)
                        
                        
                    }
                    .padding()
                    .background(Color.purple)
                    .foregroundColor(.white)
                    .cornerRadius(8)
                    .buttonStyle(FullSizeButtonStyle())
                    
                    Button("Contacts picker") {
                        showContactPicker = true
                    }
                    .padding()
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(8)
                    //
                    Button("logout") {
                        NotificationCenter.default.post(name: .logoutNotification, object: nil)
                    }
                    .padding()
                    .background(Color.orange)
                    .foregroundColor(.white)
                    .cornerRadius(8)
                    
                    Button("login Invalid") {
                        NotificationCenter.default.post(name: .loginInvalidNotification, object: nil)
                    }
                    .padding()
                    .background(Color.orange)
                    .foregroundColor(.white)
                    .cornerRadius(8)
                    
                    Button("push authen view") {
//                        navManager.wrappedValue.pushTitleView(tag: mainNav) {
//                            NDProductDetailView()
//                        }
                    }
                    .padding()
                    .background(Color.orange)
                    .foregroundColor(.white)
                    .cornerRadius(8)
                }
                .padding()
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .ignoresSafeArea()
                .sheet(isPresented: $showContactPicker) {
//                    NDContactsPicker(selectedContact: $selectedContact)
//                        .ignoresSafeArea(.all, edges: .all)
                }
                // 1. 标准 Alert
                // 1. 完全自定义弹窗（覆盖层方式）
                .ndAlertOnWindow(isPresented: $showCustomPopup, title: "test alert",
                                 tips: localizedString("logout_alert_title"),
                                 message: "please give me some time. I will give you a surprisedplease give me some time. I will give you a surprisedplease give me some time. I will give you a surprisedplease give me some time. I will give you a surprised",
                                 backgroundImage: "common_alert_bg",
                                 buttons: buttons)
                
                // 2. 带背景图片的完全自定义弹窗
                .ndCustomPopupOnWindow(isPresented: $showImagePopup, style: .alert, backgroundImage: "alert_bg_image1") {
                    VStack {
                        Text("带背景图片的弹窗")
                            .font(.headline)
                            .foregroundColor(.blue)
                            .frame(maxWidth: .infinity, minHeight: 44)
                    }
                    .padding(.horizontal, 16)
                    .frame(height: ImageUtility.proportionalHeight(for: "alert_bg_image1",
                                                                   targetWidth: AppConstants.screenWidth - 2 * 16))
                }
                
                // 3. Window 上的完全自定义弹窗
                .ndCustomPopupOnWindow(isPresented: $showWindowPopup, style: .actionSheet, backgroundImage: "address_bg", onDismissCompleted: {
                    
                    if showNextPop {
                        showWindowPopup = true
                    }
                    
                }) {
                    VStack(spacing: 20) {
                        Image(systemName: "star.fill")
                            .font(.system(size: 50))
                            .foregroundColor(.yellow)
                        
                        Text("自定义内容")
                            .font(.title2)
                            .fontWeight(.bold)
                        
                        Text("这里可以放任何 SwiftUI 视图")
                            .foregroundColor(.blue)
                        
                        HStack(spacing: 12) {
                            
                            Button {
                                showWindowPopup = false
                                showNextPop = false
                            } label: {
                                Text("取消")
                                    .font(.body)
                                    .fontWeight(.regular)
                                    .foregroundColor(Color.white)
                                    .frame(maxWidth: .infinity)
                                    .frame(height: 44)
                                    .contentShape(Rectangle())
                            }
                            .buttonStyle(PlainButtonStyle())
                            .background(Color.red)
                            .cornerRadius(8)
                            
                            Button {
                                showWindowPopup = false
                                showNextPop = true
                            } label: {
                                Text("确定")
                                    .font(.body)
                                    .fontWeight(.regular)
                                    .foregroundColor(Color.white)
                                    .frame(maxWidth: .infinity)
                                    .frame(height: 44)
                                    .contentShape(Rectangle())
                            }
                            .buttonStyle(PlainButtonStyle())
                            .background(Color.green)
                            .cornerRadius(8)
                        }
                        .padding(.horizontal, 12)
                    }
                    .padding(.horizontal, 16)
                    .frame(height: ImageUtility.proportionalHeight(for: "address_bg",
                                                                   targetWidth: AppConstants.screenWidth - 2 * 16))
                }
                
                // 4. 显示date
                .ndCustomPopupOnWindow(isPresented: $showDatePopup,
                                       style: .actionSheet,
                                       backgroundImage: "address_bg") {
                    
                    NDCalendarPicker(selectedDate: selectedDate,
                                     onDateSelected: { date in
                        selectedDate = date
                        showDatePopup = false
                    })
                    .padding(.horizontal, 5)
                    .frame(height: ImageUtility.proportionalHeight(for: "address_bg",
                                                                   targetWidth: AppConstants.screenWidth - 2 * 16))
                }
                
                // 5. 显示address
                                       .ndCustomPopupOnWindow(isPresented: $showBottomPopup,
                                                              style: .actionSheet,
                                                              backgroundImage: "address_bg") {
                                           
                                           NDAddressPickerView(
                                            data: NDAddressItem.mockProvinces,
                                            maxLevel: 3,
                                            config: NDAddressPickerConfig(
                                                title: "选择省市",
                                                provincePlaceholder: "选择省份",
                                                cityPlaceholder: "选择城市",
                                                separatorText: "→",
                                                selectedBackgroundColor: .blue.opacity(0.2),
                                                confirmButtonColor: .green,
                                                confirmButtonText: "确定",
                                                requireFullSelection: false,
                                                minSelectionLevel: 1
                                            ),
                                            actions: NDAddressPickerActions(
                                                onClickConfirmButton: { path in
                                                    print("选择结果: \(path.map { $0.name }.joined(separator: "-"))")
                                                    showBottomPopup = false
                                                },
                                                onSelectionError: { error in
                                                    print("选择错误: \(error)")
                                                }
                                            )
                                           )
                                           .padding(.horizontal, 5)
                                           .frame(height: ImageUtility.proportionalHeight(for: "address_bg",
                                                                                          targetWidth: AppConstants.screenWidth - 2 * 16))
                                       }
                
                // 6. 显示enumView
//                                                              .ndCustomPopupOnWindow(isPresented: $showEnumViewPopup,
//                                                                                     style: .actionSheet,
//                                                                                     backgroundImage: "address_bg") {
//                                                                  NDPersonalEnumView(
//                                                                    dataSource: Phasbook.mockData,
//                                                                    onSelectionChanged: { index, item in
//                                                                        print("选中了第 \(index) 项: \(item?.sarcling ?? "未知")")
//                                                                    },
//                                                                    onConfirmAction: { item in
//                                                                        print("确认选择: \(item?.sarcling ?? "未知")")
//                                                                        showEnumViewPopup = false
//                                                                    }
//                                                                  )
//                                                                  .padding(.horizontal, 5)
//                                                                  .frame(height: ImageUtility.proportionalHeight(for: "address_bg",
//                                                                                                                 targetWidth: AppConstants.screenWidth - 2 * 16))
//                                                              }
            }
        }
    }
}



